/**
 * 项目校验规则表管理初始化
 */
var SdsQcfitemMatchRule = {
    id: "SdsQcfitemMatchRuleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsQcfitemMatchRule.initColumn = function () {
    return [[
        {title: '序号', field: 'indNo', width: 50, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox'
        }},
        {title: '校验函数', field: 'controlExpId', width: 100, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                for(let j = 0,len=controlExps.length; j < len; j++) {
                    if (value == controlExps[j].id) {
                        return controlExps[j].text;
                        break;
                    }
                }
            }, editor:{
                type:'combobox',
                options:{required:true,valueField:'id',textField:'text',editable:false,
                    data: controlExps
                }
            }},
        {title: '校验表达式', field: 'controlExp', width: 150, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox'
            }},
        {title: '校验级别', field: 'controlLevel', width: 100, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                for(let j = 0,len=controlLevels.length; j < len; j++) {
                    if (value == controlLevels[j].id) {
                        return controlLevels[j].text;
                        break;
                    }
                }
            }, editor:{
                type:'combobox',
                options:{required:true,valueField:'id',textField:'text',editable:false,
                    data: controlLevels
                }
            }},
        {title: '提示内容', field: 'tipText', width: 200, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        ]];
};

/**
 * 检查是否选中
 */
SdsQcfitemMatchRule.check = function () {
    var selected = $('#' + this.id).datagrid('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsQcfitemMatchRule.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加新一行表格
 */
SdsQcfitemMatchRule.addFitemMatchRule= function () {
    var rowNum = $('#' + SdsQcfitemMatchRule.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsQcfitemMatchRule.id).datagrid('appendRow',{
        indNo: rowNum + 1,
        controlCode: '',
        controlDesc: '',
        controlExp: '',
        controlLevel: '',
        formItemId:$("#formItemId").val()
    });
    $('#' + SdsQcfitemMatchRule.id).datagrid('selectRow', rowNum).datagrid('beginEdit', rowNum);
};

/**
 * 保存数据到库表
 */
SdsQcfitemMatchRule.saveFitemMatchRule = function () {
    //保存完毕后，同步数据
    var operation_save = function(){
        SdsQcfitemMatchRule.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var rows =  $('#' + SdsQcfitemMatchRule.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row != undefined && row != {}) {
                if (row.id != undefined) {
                    //update Row
                    delete row.updateDate;
                    delete row.updateUser;
                    delete row.expDict;
                    delete row.levelDict;
                    row.controlExp=encodeURIComponent(row.controlExp);
                    Sys.ajax(Sys.backendPath + "/sds/sdsqcfitemmatchrule/update", row, function (data) {
                        Sys.success("修改成功!");
                    },function(data){
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改校验规则保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsqcfitemmatchrule/add", row, function (data) {
                        Sys.success("添加成功!");
                    },function(data){
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增校验规则保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?",operation_save);
};

/**
 * 删除行同时删除数据库表数据
 */
SdsQcfitemMatchRule.delFitemMatchRule = function () {
    if (SdsQcfitemMatchRule.check()) {
        var operation = function(){
            if(SdsQcfitemMatchRule.seItem.id!=null&&SdsQcfitemMatchRule.seItem.id!=""){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsqcfitemmatchrule/delete", function (data) {
                    Sys.success("删除成功!");
                    SdsQcfitemMatchRule.table.refresh();
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdsQcfitemMatchRuleId",SdsQcfitemMatchRule.seItem.id);
                ajax.start();
            }else{
                SdsQcfitemMatchRule.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?",operation);
    }
};

/**
 * 查询项目校验规则表列表
 */
SdsQcfitemMatchRule.search = function () {
    var queryData = {};
    queryData['formItemId'] = $("#formItemId").val();
    SdsQcfitemMatchRule.table.refresh({queryParams: queryData});
};

$(function () {
    var defaultColunms = SdsQcfitemMatchRule.initColumn();
    var table = new HISUITable(SdsQcfitemMatchRule.id, Sys.backendPath + "/sds/sdsqcfitemmatchrule/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-50);
    SdsQcfitemMatchRule.table = table.init();
});
